﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Mvc;
using CondominiosMVC.Models;
using System.Web.Script.Serialization;

namespace CondominiosMVC.Controllers
{
    public class ResidenteController : Controller
    {
        private string residenteRESTService = "http://localhost:3000/Residentes.svc/Residentes";
        JavaScriptSerializer js = new JavaScriptSerializer();

        public ActionResult Index()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(residenteRESTService);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string residenteJson = reader.ReadToEnd();
            ICollection<Residente> model = js.Deserialize<ICollection<Residente>>(residenteJson);

            return View(model);
        }

        //
        // GET: /Residente/Details/5

        public ActionResult Details(String DNI)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(residenteRESTService + "/" + DNI);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string residenteJson = reader.ReadToEnd();
            Residente model = js.Deserialize<Residente>(residenteJson);

            return View(model);
        }

        //
        // GET: /Residente/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Residente/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    string postdata = "{\"DNI\":\"" + collection["dni"] + "\",\"Nombres\":\"" + collection["Nombres"] + "\",\"ApellidoPaterno\":\"" + collection["ApellidoPaterno"] + "\",\"ApellidoMaterno\":\"" + collection["ApellidoMaterno"] + "\",\"Edad\":\"" + collection["Edad"] + "\",\"Correo\":\"" + collection["Correo"] + "\",\"Clave\":\"" + collection["Clave"] + "\",\"Tipo\":\"" + collection["Tipo"] + "\"}";
                    byte[] data = Encoding.UTF8.GetBytes(postdata);
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(residenteRESTService);
                    req.Method = "POST";
                    req.ContentLength = data.Length;
                    req.ContentType = "application/json";
                    var reqStream = req.GetRequestStream();
                    reqStream.Write(data, 0, data.Length);
                    var res = (HttpWebResponse)req.GetResponse();
                    return RedirectToAction("Index");
                }

                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError(String.Empty, "Error al crear Residente: " + e.Message);
                return View();
            }
        }
        
        //
        // GET: /Residente/Edit/5
 
        public ActionResult Edit(String DNI)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(residenteRESTService + "/" + DNI);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string residenteJson = reader.ReadToEnd();
            Residente model = js.Deserialize<Residente>(residenteJson);

            return View(model);
        }

        //
        // POST: /Residente/Edit/5

        [HttpPost]
        public ActionResult Edit(String DNI, FormCollection collection)
        {
            try
            {
                string postdata = "{\"DNI\":\"" + collection["dni"] + "\",\"Nombres\":\"" + collection["Nombres"] + "\",\"ApellidoPaterno\":\"" + collection["ApellidoPaterno"] + "\",\"ApellidoMaterno\":\"" + collection["ApellidoMaterno"] + "\",\"Edad\":\"" + collection["Edad"] + "\",\"Correo\":\"" + collection["Correo"] + "\",\"Clave\":\"" + collection["Clave"] + "\",\"Tipo\":\"" + collection["Tipo"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(residenteRESTService);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Residente/Delete/5
 
        public ActionResult Delete(String DNI)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(residenteRESTService + "/" + DNI);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string residenteJson = reader.ReadToEnd();
            Residente model = js.Deserialize<Residente>(residenteJson);

            return View(model);
        }

        //
        // POST: /Residente/Delete/5

        [HttpPost]
        public ActionResult Delete(String DNI, FormCollection collection)
        {
            try
            {
                string postdata = "\"" + DNI + "\"";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(residenteRESTService);
                req.Method = "DELETE";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
